/*
    SUSAN® - Sole of Unix Save ANything

   Copyright (C) 2002-2012 Hechzh Property
   Copyright (C) 2016-2016 Skyatlas Co. CN
   Copyright (C) 2016-2016 Skyatlas Co. CN

   
*/
/*
 * Kern Sibbald, May MMII
 */
/**
 * @file
 * SUSAN Director -- Automatic Recycling of Volumes
 *                    Recycles Volumes that have been purged
 */
#include "include/susan.h"
#include "dird.h"
#include "dird/autorecycle.h"
#include "dird/jcr_private.h"
#include "dird/next_vol.h"

namespace directordaemon {

/* Forward referenced functions */

bool FindRecycledVolume(JobControlRecord* jcr,
                        bool InChanger,
                        MediaDbRecord* mr,
                        StorageResource* store,
                        const char* unwanted_volumes)
{
  bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
  SetStorageidInMr(store, mr);
  if (jcr->db->FindNextVolume(jcr, 1, InChanger, mr, unwanted_volumes)) {
    jcr->impl->MediaId = mr->MediaId;
    Dmsg1(20, "Find_next_vol MediaId=%u\n", jcr->impl->MediaId);
    PmStrcpy(jcr->VolumeName, mr->VolumeName);
    SetStorageidInMr(store, mr);

    return true;
  }

  return false;
}

/**
 * Look for oldest Purged volume
 */
bool RecycleOldestPurgedVolume(JobControlRecord* jcr,
                               bool InChanger,
                               MediaDbRecord* mr,
                               StorageResource* store,
                               const char* unwanted_volumes)
{
  bstrncpy(mr->VolStatus, "Purged", sizeof(mr->VolStatus));
  SetStorageidInMr(store, mr);

  if (jcr->db->FindNextVolume(jcr, 1, InChanger, mr, unwanted_volumes)) {
    Dmsg1(20, "Find_next_vol MediaId=%u\n", mr->MediaId);
    SetStorageidInMr(store, mr);
    if (RecycleVolume(jcr, mr)) {
      Jmsg(jcr, M_INFO, 0, _("Recycled volume \"%s\"\n"), mr->VolumeName);
      Dmsg1(100, "return 1  RecycleOldestPurgedVolume Vol=%s\n",
            mr->VolumeName);

      return true;
    }
  }
  Dmsg0(100, "return 0  RecycleOldestPurgedVolume end\n");

  return false;
}

/**
 * Recycle the specified volume
 */
bool RecycleVolume(JobControlRecord* jcr, MediaDbRecord* mr)
{
  bstrncpy(mr->VolStatus, "Recycle", sizeof(mr->VolStatus));
  mr->VolJobs = mr->VolFiles = mr->VolBlocks = mr->VolErrors = 0;
  mr->VolBytes = 1;
  mr->FirstWritten = mr->LastWritten = 0;
  mr->RecycleCount++;
  mr->set_first_written = true;
  SetStorageidInMr(NULL, mr);

  return jcr->db->UpdateMediaRecord(jcr, mr);
}
} /* namespace directordaemon */
